/*
 * PhotoDevDAO.java
 *
 * Created on April 3, 2007, 12:46 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package finalintex2web;
//import BOs.*;
import java.sql.*;

/**
 *
 * @author Isenbargers
 */
public class PhotoDevDAO extends RevenueSourceDAO{
    
    private static PhotoDevDAO instance = null;    
    
    //~~~~~~~~~~~~~~~~~~
    //Singleton pattern
    //~~~~~~~~~~~~~~~~~~
    
    /** Creates a new instance of CustDAO */
    private PhotoDevDAO() {
    }
    
    public static synchronized PhotoDevDAO getInstance()
    {
        if(instance == null)
            instance = new PhotoDevDAO();
        return instance;
    }
    
    //----------
    //  CREATE
    //----------
    
    public PhotoDevBO create(){
        String id;
        try {
            id = GUID.generate();
        } catch (Exception ex) {
            id = "error";
            ex.printStackTrace();
        }
        PhotoDevBO newPhotoDev = new PhotoDevBO(id);
        Cache c = Cache.getInstance();
        if(!id.equals("error"))
            c.put(newPhotoDev.getId(), newPhotoDev);
        return newPhotoDev;
    }
    
    //----------
    //  READ
    //----------
    
    public synchronized PhotoDevBO read(String id) throws DataException
    {
        PhotoDevBO pdRead;
            
            //----------
            //  BAD!!!
            //----------
    //  I know this is bad, but I'm tired of banging my head on this one for now
            pdRead = PhotoDevDAO.getInstance().create();
        
        Cache c = Cache.getInstance();
        if(c.containsKey(id))
            pdRead = (PhotoDevBO) c.get(id);
        else
        {
        ConnectionPool cp = ConnectionPool.getInstance();
        try {
            Connection cxn = cp.get();
            // retrieve a database connection from the pool  --Done
            // call read with a connection (the other read method in this class)
            this.read(id, cxn);
            // release the connection
        }catch (SQLException e) {
            
            throw new DataException("Could not retrieve record for id=" + id, e);
        }catch (ConnectionPoolException e) {
            e.printStackTrace();
            
        }
        
        // release the connection back to the pool 

        }
        
        System.out.println("We've read something");
        return pdRead;           
    }
   
    
    public synchronized void/*PhotoDevBO*/ read(String id, Connection conn) throws SQLException, DataException {
           // check again if the id is in the cache, and if so,
           // just get it from the cache.  we need to check again
           // because this method might be called directly from
           // another DAO rather than from read above.
        PhotoDevBO pdRead2;
        Cache c = Cache.getInstance();
        if(c.containsKey(id))
            pdRead2 = (PhotoDevBO) c.get(id);
        else
        {}
        
           // if not in the cache, get a result set from 
           // a SELECT * FROM table WHERE id=guid
           
        
           /* if (rs.next()) {
                PhotoDevBO cst = new PhotoDevBO(rs.getString("url"), id);
                rss.setTitle(rs.getString("title"));
                MembershipBO m = MembershipDAO.getInstance().read(rs.getString("memberid"));
                rss.setMembership(m);
                rss.setAlreadyInDB(true);

                // save to the cache

            }else{
                throw new DataException("Object was not found in the database.");

            }
           
            // get any subobjects (for example, if this was the PhotoDevBODAO,
            // we'd need to call the MembershipDAO to load the membership record.
            */
        System.out.println("we're doing read2");
        //return cRead2;
    }
    
    //----------
    //  UPDATE
    //----------
    
    public synchronized void save(RevenueSourceBO pd) throws DataException {
        // check to see if id in the cache
        // if so, return it immediately
        
        try {
            ConnectionPool cp = ConnectionPool.getInstance();
            try {
                Connection cxn = cp.get();
                save(pd, cxn);
                cp.release(cxn);
            } catch (ConnectionPoolException ex) {
                ex.printStackTrace();
            } catch (DataException ex) {
                ex.printStackTrace();
            }
        }catch (SQLException ex) {
            // rollback
            throw new DataException("Could not save record for id=" + pd.getId(), ex);
        }
        
    }
    synchronized void save(RevenueSourceBO pd, Connection conn) throws SQLException, DataException {
        if (pd.isDirty()) {
            if (pd.isIndb()) {
                update(pd, conn);
            }else{
                insert(pd, conn);
            }
            pd.setDirty(false);
        }
        
        // call save(bo, conn) on any subobjects (like PhotoDevBODAO to MembershipDAO)
        // touch the cache for the object
    }
    
    private synchronized void update(RevenueSourceBO pd, Connection cn) throws SQLException, DataException {
        System.out.println("update time");
        Statement stmt = cn.createStatement();
            //String newVals = "cName = \'"+cst.getName()+"\', cPhone = \'"+ cst.getPhone()+"\', cEmail = \'"+cst.getEmail()+"\', cAddress = \'"+cst.getAddress()+"\'";
            //String guard = " WHERE cID = \'" + cst.getId() + "\'";
            String doIt = "UPDATE PhotoDevBO SET " /*+ newVals + guard*/;
			
            stmt.executeUpdate(doIt);
            
    }
    
    private synchronized void insert(RevenueSourceBO pd, Connection cn) throws SQLException, DataException {
        try {
            // do the insert SQL statement
            Statement stmt = cn.createStatement();
            //String pdVals = "(\'"+pd.getId()+"\', \'"+pd.getName()+"\', \'"+ pd.getPhone()+"\', \'"+cst.getEmail()+"\', \'"+cst.getAddress()+"\')";
            String doIt = "INSERT INTO PhotoDevBO "/*(cID, cName, cPhone, cEmail, cAddress) VALUES " + cstVals*/;
			
            stmt.executeUpdate(doIt);
            // tell the object that it's now in the db (so we call update next time not insert)

            pd.setIndb(true);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
   
    //----------
    //  DELETE
    //----------
    
        /*This function is not supported as we will want to maintain PhotoDevBO information
            in the database.  Storing "inactive" PhotoDevBOs also allows us to access 
            information on past transactions without affecting the readability of other 
            objects.
        */
    
    
    //----------
    //  SEARCH
    //----------
 /*   
    public List<String[]> getAll() throws DataException {
        // get the connection
        
        // sql the names, phone, and ids
        
        // while loop to populate the list from the results
        
        // release my connection
        
        // return the list
    }
    
    public List<PhotoDevBO> getByName(String fname, String lname) throws DataException {
        // similar to getAll
    }
    
    public List<RSSFeedBO> getByPhone(String phoneNumber) throws DataException {
        // similar to getByPhone
    }
   */ 
}
